package com.cw.jeeyt.service.impl.process;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.converter.process.ActDeModelHistoryConverter;
import com.cw.jeeyt.service.intf.process.ActDeModelHistoryService;
import com.cw.lang.common.utils.SequenceTool;
import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.process.ActDeModelHistoryBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.process.ActDeModelHistoryDao;
import com.cw.jeeyt.dao.model.process.ActDeModelHistoryDo;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 流程定义历史业务实现类
 *
 * @author chenw
 * @date 2019-03-10 21:47:04
 */
@Service
public class ActDeModelHistoryServiceImpl implements ActDeModelHistoryService {

    private static final Logger logger = LoggerFactory.getLogger(ActDeModelHistoryServiceImpl.class);
    @Autowired
    private ActDeModelHistoryDao actDeModelHistoryDao;//流程定义历史数据库操作接口

    /**
     * 查询所有流程定义历史
     */
    @Override
    public List<ActDeModelHistoryBo> queryActDeModelHistoryList() throws Exception {
        return ActDeModelHistoryConverter.doList2BoList(actDeModelHistoryDao.queryActDeModelHistoryList());
    }

    /**
     * 分页查询流程定义历史
     *
     * @param actDeModelHistoryBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ActDeModelHistoryBo> queryByPage(ActDeModelHistoryBo actDeModelHistoryBo, Integer startNum,
        Integer pageSize, Order order) throws Exception {

        if (actDeModelHistoryBo == null) {
            logger.error("流程定义历史为空");
            return null;
        }
        PageInfo<ActDeModelHistoryDo> pageInfo = actDeModelHistoryDao.queryByPage(
            ActDeModelHistoryConverter.bo2Do(actDeModelHistoryBo), startNum, pageSize, order);
        return new PageInfo<>(pageInfo, ActDeModelHistoryConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查询流程定义历史信息
     *
     * @return
     */
    @Override
    public ActDeModelHistoryBo getActDeModelHistoryById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }
        return ActDeModelHistoryConverter.do2Bo(actDeModelHistoryDao.getById(id));
    }

    /**
     * 新增流程定义历史
     *
     * @param actDeModelHistoryBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(ActDeModelHistoryBo actDeModelHistoryBo) throws Exception {

        if (actDeModelHistoryBo == null) {
            logger.error("流程定义历史为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActDeModelHistoryDo actDeModelHistory = ActDeModelHistoryConverter.bo2Do(actDeModelHistoryBo);
        actDeModelHistory.setId(SequenceTool.nextId());
        actDeModelHistory.setCreatedBy(userBo.getId()
            .toString());
        actDeModelHistory.setCreated(new Date());
        actDeModelHistory.setLastUpdatedBy(userBo.getId()
            .toString());
        actDeModelHistory.setLastUpdated(new Date());
        Integer result = actDeModelHistoryDao.insert(actDeModelHistory);
        return result;
    }

    /**
     * 新增流程定义历史不为空的字段
     *
     * @param actDeModelHistoryBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ActDeModelHistoryBo actDeModelHistoryBo) throws Exception {

        if (actDeModelHistoryBo == null) {
            logger.error("流程定义历史为空");
            return null;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActDeModelHistoryDo actDeModelHistory = ActDeModelHistoryConverter.bo2Do(actDeModelHistoryBo);
        actDeModelHistory.setId(SequenceTool.nextId());
        actDeModelHistory.setCreatedBy(userBo.getId()
            .toString());
        actDeModelHistory.setCreated(new Date());
        actDeModelHistory.setLastUpdatedBy(userBo.getId()
            .toString());
        actDeModelHistory.setLastUpdated(new Date());
        Integer result = actDeModelHistoryDao.insertSelective(actDeModelHistory);
        return result;
    }

    /**
     * 更新流程定义历史
     *
     * @param actDeModelHistoryBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ActDeModelHistoryBo actDeModelHistoryBo) throws Exception {

        if (actDeModelHistoryBo == null) {
            logger.error("流程定义历史为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActDeModelHistoryDo actDeModelHistory = ActDeModelHistoryConverter.bo2Do(actDeModelHistoryBo);
        actDeModelHistory.setLastUpdatedBy(userBo.getId()
            .toString());
        actDeModelHistory.setLastUpdated(new Date());
        actDeModelHistoryDao.update(actDeModelHistory);
    }

    /**
     * 更新流程定义历史不为空的字段
     *
     * @param actDeModelHistoryBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ActDeModelHistoryBo actDeModelHistoryBo) throws Exception {

        if (actDeModelHistoryBo == null) {
            logger.error("流程定义历史为空");
            return;
        }
        SecurityUserBo userBo = SessionUtil.getSessionUser();
        if (userBo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ActDeModelHistoryDo actDeModelHistory = ActDeModelHistoryConverter.bo2Do(actDeModelHistoryBo);
        actDeModelHistory.setLastUpdatedBy(userBo.getId()
            .toString());
        actDeModelHistory.setLastUpdated(new Date());

        actDeModelHistoryDao.updateSelective(actDeModelHistory);
    }

    /**
     * 删除流程定义历史
     *
     * @param actDeModelHistoryBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ActDeModelHistoryBo actDeModelHistoryBo) throws Exception {

        if (actDeModelHistoryBo == null) {
            logger.error("流程定义历史为空");
            return;
        }
        ActDeModelHistoryDo actDeModelHistory = ActDeModelHistoryConverter.bo2Do(actDeModelHistoryBo);
        actDeModelHistoryDao.delete(actDeModelHistory);
    }
}
